package com.isias.system.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isias.common.result.Result;
import com.isias.model.El.ElPaper;
import com.isias.model.El.ElUserPaperAnswer;
import com.isias.model.dto.ElUserPaperDto;
import com.isias.model.dto.WorkDetailDto;
import com.isias.model.vo.ElPaperQueryVo;
import com.isias.model.vo.WorkDetailVo;
import com.isias.system.Service.IAuthenticationFacade;
import com.isias.system.service.ElUserPaperAnswerService;
import com.isias.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value="/admin/el/userPaper")
public class ElUserPaperAnswerController {
    @Autowired
    private ElUserPaperAnswerService elRepoService;

    @Autowired
    private IAuthenticationFacade authenticationFacade; //获取当前用户对象类

    @Autowired
    private SysUserService sysUserService;

    /***
     * 根据用户来创建试卷
     * @param paperId
     * @return
     */
    @GetMapping("{paperId}")
    public Result CreatPaperToUser(@PathVariable String paperId){
        return Result.ok(elRepoService.CreatPaperToUser(GetId(),paperId));
    }

    /***
     * 用el_user_paper_id来获取题目信息，返回给页面（不用paperid直接获取是因为方便后面调用，以及写完答案后回显操作）
     * @param elUserPaperId
     * @return
     */
    @GetMapping("/init/{elUserPaperId}")
    public Result InitQu(@PathVariable String elUserPaperId){
        return Result.ok(elRepoService.InitQu(elUserPaperId));
    }

    /**
     * 获取单个题目(已被废弃)
     * @param elUserPaperId
     * @param quId
     * @return
     */
    @GetMapping("{elUserPaperId}/{quId}")
    public Result GetOneQu(@PathVariable String elUserPaperId,@PathVariable String quId){
        return Result.ok(elRepoService.GetOneQu(elUserPaperId,quId));
    }

    /**
     * 交卷操作（批量更新）,并且计算分数
     * @param list
     * @return
     */
    @PutMapping("{paperId}/{elUserPaperId}")
    public Result UpdateList(@RequestBody List<ElUserPaperAnswer> list,@PathVariable String paperId,@PathVariable String elUserPaperId){
        if (elRepoService.UpdateList(list,paperId,elUserPaperId)){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 刷新成绩
     * @param elUserPaperId
     * @return
     */
    @GetMapping("/UpdatePoint/{elUserPaperId}")
    public Result UpdatePoint(@PathVariable String elUserPaperId){
        elRepoService.UpdatePoint(elUserPaperId);
        return Result.ok();
    }

    /**
     * 获取当前登录用户的作业
     * @return
     */
    Page<ElUserPaperDto> pageparam = new Page<>();
    @GetMapping("Mywork/{page}/{limit}")
    public Result Mywork(@PathVariable Integer page, @PathVariable Integer limit,ElPaperQueryVo elPaperQueryVo){
        pageparam.setCurrent(page);
        pageparam.setSize(limit);
        IPage<ElUserPaperDto> iPage = elRepoService.Mywork(pageparam,GetId(),elPaperQueryVo);
        return Result.ok(iPage);
    }

    /***
     * 根据某个作业id来获取所有写这个卷子的人
     * @param page
     * @param limit
     * @param paperId
     * @param workDetailVo
     * @return
     */
    Page<WorkDetailDto> param = new Page<>();
    @GetMapping("workDetail/{page}/{limit}/{paperId}")
    public Result workDetail(@PathVariable Integer page, @PathVariable Integer limit, @PathVariable String paperId, WorkDetailVo workDetailVo){
        pageparam.setCurrent(page);
        pageparam.setSize(limit);
        IPage<WorkDetailDto> iPage = elRepoService.workDetail(param,paperId,workDetailVo);
        return Result.ok(iPage);
    }

    /**
     * 打分
     * @param elUserPaperId
     * @param point
     * @return
     */
    @PutMapping("/updateGetPoint/{elUserPaperId}/{point}")
    public Result updateGetPoint(@PathVariable String elUserPaperId,@PathVariable int point){
        if (elRepoService.updateGetPoint(elUserPaperId,point)) {
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 获取当前操作用户的id抽取方法
     * @return
     */
    public String GetId(){
        // 获取当前登录用户
        Authentication authentication = authenticationFacade.getAuthentication();
        String username = authentication.getName();
        return sysUserService.GetIdByUsername(username);
    }
}
